package com.yupi.springbootinit.Crawler;
import java.io.IOException;
import java.util.ArrayList;
import cn.hutool.http.HttpRequest;
import cn.hutool.json.*;
import com.yupi.springbootinit.model.entity.Picture;
import com.yupi.springbootinit.model.entity.Post;
import com.yupi.springbootinit.service.PostService;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;
import org.junit.jupiter.api.Test;
import org.springframework.boot.test.context.SpringBootTest;
import org.jsoup.Jsoup;
import javax.annotation.Resource;
import java.util.List;
import java.util.Map;

import static org.bouncycastle.asn1.x500.style.RFC4519Style.o;
import static org.bouncycastle.asn1.x500.style.RFC4519Style.title;

@SpringBootTest
public class crawler {

    @Resource
    private PostService postService;

    @Test
    public void TestCrawler(){
        String json ="{\"pageSize\":12,\"sortOrder\":\"descend\",\"tags\":[\"Java\"],\"current\":1,\"reviewStatus\":1,\"category\":\"文章\",\"hiddenContent\":true,\"priorityList\":[999,9999],\"sorterList\":[{\"field\":\"priority\",\"asc\":false},{\"field\":\"createTime\",\"asc\":false}]}";
        String url  = "https://api.code-nav.cn/api/course/list/page/vo";
        String result = HttpRequest.post(url)
                .body(json)
                .execute().body();
        System.out.println(result);
        Map<String,Object> map = JSONUtil.toBean(result,Map.class);
        JSONObject data = (JSONObject)map.get("data");
        JSONArray records = (JSONArray) data.get("records");
        //1:title 2:content 3:tag
        List<Post> postList = new ArrayList<>();
        for (Object record : records) {
            JSONObject tempjson = (JSONObject)record;
            Post post = new Post();
            post.setTitle(tempjson.getStr("title"));
            post.setContent(tempjson.getStr("content"));
            Object tags = tempjson.get("tags");
            if(!(tags instanceof JSONNull)){
                JSONArray jsonArray = (JSONArray) tempjson.get("tags");
                List<String> tagList = jsonArray.toList(String.class);
                post.setTags(JSONUtil.toJsonStr(tagList));
            }else {
                post.setTags("");
            }
            post.setUserId(1L);
            postList.add(post);
        }
        final boolean save = postService.saveBatch(postList);
//        System.out.println(save);
    }


    @Test
    public void JsoupTest() throws IOException {
        String current = "1";
        String url = "https://cn.bing.com/images/search?q=小黑子&form=HDRSC2&first="+current;
        Document doc = Jsoup.connect(url).get();
        Elements elements = doc.select(".iusc");
        final Elements elements1 = doc.select("a.inflnk");
        List<Picture> pictureList = new ArrayList<>();
        for (int i = 0; i < elements.size(); i++) {
            //获取图片
            final String m = elements.get(i).attr("m");
            Map<String,Object> map = JSONUtil.toBean(m,Map.class);
            final String murl = (String) map.get("murl");
            //获取标题
            final String title = elements1.get(i).attr("aria-label");
            Picture picture = new Picture();
            picture.setMurl(murl);
            picture.setTitle(title);
            pictureList.add(picture);
        }
    }

}
